package cn.life.business.warehouse.dao.impl;

import cn.life.business.warehouse.dao.WarehouseDao;
import cn.life.business.warehouse.entity.po.Warehouse;
import cn.life.business.warehouse.entity.po.WarehouseDetails;
import cn.life.business.warehouse.entity.vo.WarehouseDetailsVo;
import cn.life.business.warehouse.entity.vo.WarehouseVo;
import cn.life.common.entity.vo.Pagination;
import cn.life.common.support.jdbc.AbstractSupportDao;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 王振宇
 * @Description:
 * @Date 2018-07-31 14:31:32
 */
@Repository
public class WarehouseDaoImpl extends AbstractSupportDao implements WarehouseDao {

    @Override
    public Long addWarehouse(Warehouse warehouse) {
        Long id=queryTableNextId("factory_warehouse","warehouse_id");
        String sql= "INSERT INTO factory_warehouse(warehouse_id,material_desc,state,project_id,project_name,opr_user_id,opr_user_name,opr_date,apply_name,json) VALUES(?,?,?,?,?,?,?,?,?,?)";
        return update(sql,id,warehouse.getMaterialDesc(),warehouse.getState(),warehouse.getProjectId(),
                warehouse.getProjectName(),warehouse.getOprUserId(),warehouse.getOprUserName(),warehouse.getOprDate(),warehouse.getApplyName(),warehouse.getJson()) == 1 ? id : null;
    }

    @Override
    public int addWarehouseDetails(Warehouse warehouse) {
        String sql= "INSERT INTO warehouse_material_detail(material_id,material_name,material_number,warehouse_id) VALUES(?,?,?,?)";
        List<Object[]> list = new ArrayList<>();
        for(WarehouseDetails warehouseDetails1 :warehouse.getList()){
            Object[] objects = new Object[4];
            objects[0]=warehouseDetails1.getMaterialId();
            objects[1]=warehouseDetails1.getMaterialName();
            objects[2]=warehouseDetails1.getMaterialNumber();
            objects[3]=warehouse.getWarehouseId();
            list.add(objects);
        }
        batchUpdate(sql,list);
        return 1;
    }

    @Override
    public int deleteWarehouse(Warehouse warehouse) {
        String sql= "DELETE from factory_warehouse where warehouse_id=?";
        return update(sql,warehouse.getWarehouseId());
    }

    @Override
    public int deleteWarehouseDetails(Warehouse warehouse) {
        String sql= "DELETE from warehouse_material_detail where warehouse_id=?";
        return  update(sql,warehouse.getWarehouseId());
    }

    @Override
    public int updateWarehouse(Warehouse warehouse) {
        String sql= "UPDATE factory_warehouse  set material_desc=?,state=? WHERE warehouse_id=? ";
        return update(sql,warehouse.getMaterialDesc(),warehouse.getState(),warehouse.getWarehouseId());
    }

    @Override
    public int updateWarehouseDetails(WarehouseDetails warehouseDetails) {
        String sql= "UPDATE warehouse_material_detail  set material_id=?,material_number=?,material_name=? WHERE detail_id=? ";
        return update(sql,warehouseDetails.getMaterialId(),warehouseDetails.getMaterialNumber(),warehouseDetails.getMaterialName(),warehouseDetails.getDetailId());
    }

    @Override
    public List<WarehouseDetailsVo> queryWarehouseDetailList(WarehouseVo warehouseVo) {//SELECT * FROM factory_warehouse,warehouse_material_detail WHERE factory_warehouse.warehouse_id = warehouse_material_detail.warehouse_id
        String sql = "select a.*,b.material_type_name as materialTypeName,b.material_desc as materialDesc from warehouse_material_detail a left join material_info b on a.material_id = b.material_id where a.warehouse_id = ?";
        return queryForList(sql,WarehouseDetailsVo.class,warehouseVo.getWarehouseId());
    }

    @Override
    public Pagination<WarehouseVo> select(WarehouseVo warehouseVo) {//SELECT * FROM factory_warehouse,warehouse_material_detail WHERE factory_warehouse.warehouse_id = warehouse_material_detail.warehouse_id
        StringBuffer stringBuffer=new StringBuffer(" SELECT fw.state,wmd.material_name,wmd.material_number,mi.material_type_name,mi.material_desc from  factory_warehouse fw , warehouse_material_detail wmd ,material_info mi where fw.warehouse_id=wmd.warehouse_id AND mi.material_id=wmd.material_id and fw.warehouse_id=? ");
        if (warehouseVo.getState()!=0){
            stringBuffer.append(" and state ="+warehouseVo.getState()+" ");
        }
        if(warehouseVo.getProjectId() != null && !warehouseVo.getProjectId().equals("")){
            stringBuffer.append(" and project_id ="+warehouseVo.getProjectId()+" ");
        }
        return queryForListByPageEx(stringBuffer.toString(),WarehouseVo.class,warehouseVo.getPageIndex(),warehouseVo.getPageSize(),warehouseVo.getWarehouseId());
    }

    @Override
    public List<WarehouseDetailsVo> getWarehouseDetails(WarehouseVo warehouseVo) {
        String sql= "SELECT * from  warehouse_material_detail where warehouse_id=? ";
        return queryForListByPage(sql,WarehouseDetailsVo.class,warehouseVo.getPageIndex(),warehouseVo.getPageSize(),warehouseVo.getWarehouseId());
    }

    @Override
    public WarehouseVo getWarehouse(WarehouseVo warehouseVo) {
        String sql= "SELECT * from  factory_warehouse  where warehouse_id=? ";
        return queryForObject(sql,WarehouseVo.class,warehouseVo.getWarehouseId());
    }

    @Override
    public Pagination<WarehouseVo> getWarehouseByState(WarehouseVo warehouseVo) {//SELECT * from factory_warehouse fw ,warehouse_material_detail wmd where fw.warehouse_id=wmd.warehouse_id and wmd.material_name='小型金属材料'
        StringBuffer stringBuffer=new StringBuffer(" SELECT fw.*,p.project_name as projectName,group_concat(distinct wmd.material_name) as materialNames from factory_warehouse fw ,warehouse_material_detail wmd,project_info p where fw.project_id = p.project_id and fw.warehouse_id=wmd.warehouse_id  ");
        if (warehouseVo.getState()!=0){
            stringBuffer.append(" and fw.state ="+warehouseVo.getState()+" ");
        }
        if (warehouseVo.getProjectId()!=null && warehouseVo.getProjectId()!=0){
            stringBuffer.append(" and fw.project_id ="+warehouseVo.getProjectId()+" ");
        }
        if ((!"".equals(warehouseVo.getMaterialName())&&warehouseVo.getMaterialName()!=null) && (!"".equals(warehouseVo.getApplyName())&&warehouseVo.getApplyName()!=null)){
            stringBuffer.append(" and (wmd.material_name like'%"+warehouseVo.getMaterialName()+"%' or fw.apply_name like '%"+warehouseVo.getApplyName()+"%') ");
        }
        stringBuffer.append("GROUP BY fw.warehouse_id ORDER BY opr_date DESC");

        return queryForListByPageEx(stringBuffer.toString(),WarehouseVo.class,warehouseVo.getPageIndex(),warehouseVo.getPageSize());
    }

}
